package org.vestbjergbyggecenter.TUI;
import java.util.*;

import org.vestbjergbyggecenter.Controller.OrderCtr;



public class OrderMenu {
	 private OrderCtr orderCtr;
	 private static OrderMenu instance = null;
	 
	    private OrderMenu()
	    {
	        orderCtr = OrderCtr.getInstance();
	        start();
	    }
	    
	    public static OrderMenu getInstance()
	    {
	        if(instance == null)
	        {
	            instance = new OrderMenu();
	        }
	        return instance;
	    }
	    
	    // start menu
	    public void start()
	    {
	        boolean exit = false;
	        while(!exit)
	        {
	            int choice = printOrderMenu();
	            switch (choice)
	            {
	                case 1: listAll();
	                        break;
	                case 2: createOrder();
	                        break;
	                case 3: readOrder();
	                        break;
	                case 4: updateOrder();
	                        break;
	                case 5: deleteOrder();
	                        break;
	                case 6: goBack();
	                        break;
	            }
	        }
	    }
	    
	    public int printOrderMenu()
	    {
	        Scanner keyboard = new Scanner(System.in);
	        //System.out.print('\f');
	        System.out.println("***Order Menu***");
	        System.out.println("1. List all orders");
	        System.out.println("2. Create order");
	        System.out.println("3. Read order");
	        System.out.println("4. Update order");
	        System.out.println("5. Delete order");
	        System.out.println("6. Go back");
	        System.out.println("Choice #: ");
	        
	        int choice = keyboard.nextInt();
	        return choice;
	    }
	    
	    public void listAll()
	    {
	        System.out.print('\f');
	        orderCtr.listAllOrders();
	    }
	    
	    
	    public void createOrder()
		{
		        Scanner keyboard = new Scanner(System.in); 
		        System.out.print('\f');
		        System.out.println("The id of the order: ");
		        int id = Integer.parseInt(keyboard.nextLine());
		        System.out.println("The status of the order: ");
		        String status = keyboard.nextLine();
		        System.out.println("Delivery date/Return date: ");
		        long deliveryDate = Long.parseLong(keyboard.nextLine());
		        System.out.println("The customer id is: ");
		        int customerId = Integer.parseInt(keyboard.nextLine());
		        System.out.println("The SalesAssistant id is: ");
		        int salesAssistant = Integer.parseInt(keyboard.nextLine());		        
		        
		        orderCtr.createOrder(id, deliveryDate, status,  customerId, salesAssistant);  
		        addProducts(id);
	    }
	    
	    public void addProducts(int orderId)
	    {
	    	boolean quit=false;
	    	Scanner keyboard = new Scanner(System.in); 
		    System.out.print('\f');
		    System.out.println("Add Products!");
		    while(!quit){
		    	System.out.println("Select product: ");
		    	int id = Integer.parseInt(keyboard.nextLine());
		    	/*System.out.println("How much of this product: ");
		    	int quantity = Integer.parseInt(keyboard.nextLine());
		    	orderCtr.createSubOrder(orderId, id, quantity);*/
		    	orderCtr.addProduct(id, orderId);
		        System.out.println("Would you like to add more? (Yes/No) ");
		        String status = keyboard.nextLine();
		        if(status=="No")
		        {	        	
		        	readOrder(id);
		        	quit=true;
		        	break;
		        }		        	
		    }
	    }
	    
	    public void readOrder(int id)
	    {	        
	        orderCtr.printOrder(id);
	    }
	    
	    public void readOrder()
	    {
	        Scanner keyboard = new Scanner(System.in);
	        System.out.print('\f');
	        System.out.println("Type order id: ");
	        int id = keyboard.nextInt();
	        
	        orderCtr.printOrder(id);
	    }
	    
	    
	    
	    public void updateOrder()
	    {
	        Scanner keyboard = new Scanner(System.in);
	        System.out.print('\f');
	        System.out.println("Type order ID number: ");
	        int id = Integer.parseInt(keyboard.nextLine());	        
	        System.out.println("Type new  delivery date (in case you don't want to change it press enter)");
	        long deliveryDate = Long.parseLong(keyboard.nextLine());
	        System.out.println("Type new  status (in case you don't want to change it press enter)");
	        String status = keyboard.nextLine();
	        
	        
	        orderCtr.updateOrder(id, deliveryDate, status);
	    }
	    public void deleteOrder()
	    {
	        Scanner keyboard = new Scanner(System.in);
	        System.out.print('\f');
	        System.out.println("Remove order: ");
	        int id = Integer.parseInt(keyboard.nextLine());
	        orderCtr.deleteOrder(id);
	    }
	    
	   
	    
	    // calls and returns to main menu
	    public void goBack()
	    {
	        MainMenu.getInstance().start();
	    }
	}



